package com.yunzo.coc.api;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.yunzo.coc.common.bean.*;
import com.yunzo.coc.common.bean.coc.GroupEcOrder;
import com.yunzo.coc.common.bean.coc.GroupMemberInfoEntey;
import com.yunzo.coc.common.bean.coc.GroupMemberSysSmsListEntey;
import com.yunzo.coc.common.bean.coc.MemberFriendsAddsmsEntey;
import com.yunzo.coc.common.bean.coc.MemberFriendsEntey;
import com.yunzo.coc.common.bean.coc.MyGroupInfoEntey;
import com.yunzo.coc.common.bean.param.Feedback;
import com.yunzo.coc.common.bean.param.MemberCompanyinfoEntry;
import com.yunzo.coc.common.bean.param.MemberProfile;
import com.yunzo.coc.common.bean.param.SeachValue;
import com.yunzo.coc.common.utils.SMSUtils;
import com.yunzo.coc.common.Require;
import com.yunzo.coc.common.token.TokenManager;
import com.yunzo.coc.common.utils.DateUtils;
import com.yunzo.coc.common.utils.PushUtils;
import com.yunzo.coc.common.utils.RandomUtils;
import com.yunzo.coc.common.utils.StringUtils;
import com.yunzo.coc.config.AppProperty;
import com.yunzo.coc.interceptor.TokenInterceptor;
import com.yunzo.coc.model.BasicApploginModel;
import com.yunzo.coc.model.BasicSmsauthcodeModel;
import com.yunzo.coc.model.GroupContactsModel;
import com.yunzo.coc.model.GroupEcprodOrderModel;
import com.yunzo.coc.model.GroupEcprodinfoModel;
import com.yunzo.coc.model.GroupInfoModel;
import com.yunzo.coc.model.GroupMemberCompanyModel;
import com.yunzo.coc.model.GroupMemberModel;
import com.yunzo.coc.model.GroupMemberModelFriends;
import com.yunzo.coc.model.GroupMemberModelFriendsSms;
import com.yunzo.coc.model.GroupMemberinfoModel;
import com.yunzo.coc.model.SystemJpushUrlInfoModel;
import com.yunzo.coc.model.SystemJpushUserReadInfoModel;
import com.yunzo.coc.model.SystemPointlikeModel;
import com.yunzo.coc.model.SystemReviewModel;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户账号相关的接口*
 *http://localhost:8080/cocapi/api/account/login?mid=13540148430&paswd=888888&groupid=88888888888888888888888888888888&type=0
 *http://localhost:8080/cocapi/api/account/login?mid=13540148430&paswd=888888&groupid=88888888888888888888888888888888&type=0
 * 检查账号是否被注册: GET /api/account/checkUser 
 * 发送注册验证码: POST /api/account/sendCode 
 * 注册:POST /api/account/register 
 * 登录： POST /api/account/login 
 * 查询用户资料: GET /api/account/profile 
 * 修改用户资料: POST /api/account/profile 
 * 修改密码: POST /api/account/password 
 * 修改头像: POST /api/account/avatar 
 * 会员找回密码 : POST /api/account/findpaswdbymidandcode
 * 
 * @author xiaobo
 */
public class AccountAPIController extends BaseAPIController {
	private static Logger log = Logger.getLogger(AccountAPIController.class);
	
	/**
	 * 检查用户账号是否被注册*
	 */
	public void checkUser() {
		String loginName = getPara("mids");
		if (StringUtils.isEmpty(loginName)) {
			renderArgumentError("mids can not be null");
			return;
		}
		// 检查手机号码是否被注册
		boolean exists = Db.findFirst(
				MEMBERMIDSSQL, loginName) != null;
		renderJson(new BaseResponse(exists ? Code.SUCCESS : Code.FAIL,
				exists ? "registered" : "unregistered"));
	}

	/**
	 *  发送短信验证码 
	 */
	public void sendCode() {
		String loginName = getPara("regmid");
		String groupid=getPara("groupid");
		int type = getParaToInt("type", 0);// 验证码数据  0 普通发送验证码 1 登陆方式验证手机是否是团体会员
		
		if (StringUtils.isEmpty(loginName)) {
			renderArgumentError("手机号码不能为空!");
			return;
		}
		// 检查手机号码有效性
		if (!SMSUtils.isMobileNo(loginName)) {
			renderArgumentError("手机号码非法！");
			return;
		}
		if(type!=0)
		{
			GroupMemberModel nowUser=GroupMemberModel.dao.findFirst(MEMBERMIDSSQL_GROUP,loginName,groupid);
			// 检查账户是否已被注册
			if (nowUser == null) {
				renderJson(new BaseResponse(Code.ACCOUNT_NO_EXISTS,"您的手机号码未在本商会注册！请先注册或以游客方式进入体验！！"));
						return;
			}
		}
		String smsCode = SMSUtils.randomSMSCode(6);
		// 发送短信验证码
		if (!SMSUtils.sendCode(loginName,smsCode)) {
			renderFailed("短信发送失败！");
			return;
		}

		// 保存验证码数据
		BasicSmsauthcodeModel registerCode = new BasicSmsauthcodeModel()
				.set(BasicSmsauthcodeModel.ID, RandomUtils.randomCustomUUID())
				.set(BasicSmsauthcodeModel.VERSION, 0)
				.set(BasicSmsauthcodeModel.MOBILE, loginName)
				.set(BasicSmsauthcodeModel.CODE, smsCode)
				.set(BasicSmsauthcodeModel.CREATETIME, DateUtils.getDate());
		// 保存数据
		if (Db.findFirst(SMSAUTHCODEUSERSQL,
				loginName) == null) {
			registerCode.save();
		} else {
			Db.update(UPDSMSAUTHCODESQL,smsCode,loginName);
		}

		renderJson(new BaseResponse("短信发送成功！"));

	}

	/**
	 * 用户注册
	 */
	public void register() {
		// 必填信息
		String loginName = getPara("mid");// 登录帐号
		int code = getParaToInt("code", 0);// 手机验证码

		// 校验必填项参数
		if (!notNull(Require.me().put(loginName, "手机号码不能为空")
				.put(code, "手机验证码不能为空")// 根据业务需求决定是否使用此字段
		)) {
			return;
		}
		// 检查手机号码有效性
		if (!SMSUtils.isMobileNo(loginName)) {
			renderArgumentError("手机号码非法！");
			return;
		}
		// 检查账户是否已被注册
		if (Db.findFirst(MEMBERMIDSSQL, loginName) != null) {
					renderJson(new BaseResponse(Code.ACCOUNT_EXISTS,
							"您的手机号码已经注册了！请直接使用！"));
					return;
		}

		// 检查验证码是否有效
		if (Db.findFirst(SMSAUTHCODESQL,loginName, code) == null) {
			renderJson(new BaseResponse(Code.CODE_ERROR, "验证码无效！"));
			return;
		}

//		new GroupMemberModel()
//				.set(GroupMemberModel.ID, RandomUtils.randomCustomUUID())
//				.set(GroupMemberModel.MID, loginName)
//				.set(GroupMemberModel.PWD,StringUtils.encodePassword("888888", "md5")).save();
//		
//		new GroupMemberinfoModel()
//		.set(GroupMemberinfoModel.ID, RandomUtils.randomCustomUUID())
//		.set(GroupMemberinfoModel.MID, loginName)
//		.set(GroupMemberinfoModel.REALNAME,"用户"+SMSUtils.randomSMSCode(6))
//		.set(GroupMemberinfoModel.SEX, "男")
//		.save();
		String temp=RandomUtils.randomCustomUUID();
		new GroupMemberModel()
		.set(GroupMemberModel.ID, temp)
		.set(GroupMemberModel.MID, loginName)
		.set(GroupMemberModel.PWD,StringUtils.encodePassword("888888", "md5"))
		.set(GroupMemberModel.REALNAME,"用户"+SMSUtils.randomSMSCode(6))
		.set(GroupMemberModel.SEX, "男")
		.save();

		new GroupMemberinfoModel()
		.set(GroupMemberinfoModel.ID, RandomUtils.randomCustomUUID())
		.set(GroupMemberinfoModel.MID, loginName)
		.set(GroupMemberinfoModel.MEMBERID, temp)
		.save();
		

		// 删除验证码记录
		Db.update(DELSMSAUTHCODESQL,loginName, code);

		// 返回数据
		renderJson(new BaseResponse("你已经注册成功！！"));
	}
	
	/**
	 * 验证码登陆方式
	 */
	public void apptogrouploginformodilecode()
	{
		// 必填信息
		String loginName = getPara("mid");// 登录帐号
		int code = getParaToInt("code", 0);// 手机验证码
		String groupid = getPara("groupid");//团体ID
		int type= getParaToInt("type", 0);// 登陆类型 0会员 1游客
		LoginResponse response = new LoginResponse();
	    LoginResponseEntery login=null;
	    
		// 校验必填项参数
		if (!notNull(Require.me().put(loginName, "手机号码不能为空!").put(code, "验证码不能为空!").put(groupid, "团体ID不能为空!"))) {
			return;
		}
		
		// 检查验证码是否有效, 如果业务不需要，则无需保存此段代码
		if (Db.findFirst(SMSAUTHCODESQL,loginName, code) == null) {
			renderJson(new BaseResponse(Code.CODE_ERROR, "验证码非法！！！"));
			return;
		}
		GroupMemberModel nowUser=GroupMemberModel.dao.findFirst(MEMBERMIDSSQL_GROUP,loginName,groupid);
		// 检查账户是否已被注册
		if (nowUser == null) {
			renderJson(new BaseResponse(Code.ACCOUNT_NO_EXISTS,"您的手机号码未在本商会注册！请先注册或以游客方式进入体验！！"));
					return;
		}else
		{
			 GroupMemberinfoModel userinfo=GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,loginName);
			 GroupInfoModel gm=GroupInfoModel.dao.findById(groupid);
			 login=new LoginResponseEntery();
			 if(userinfo!=null)
			 {
				type=0;
				 GroupContactsModel gcm=GroupContactsModel.dao.findFirst(GROUPCONTACTSBYGROUPIDANDMIDSSQL, groupid,loginName);
				 login.setN0(loginName);
//				 login.setN1(userinfo.getStr(GroupMemberinfoModel.HEADIMAGE));
//				 login.setN2(userinfo.getStr(GroupMemberinfoModel.REALNAME));
//				 login.setN3(userinfo.getStr(GroupMemberinfoModel.SEX));
				 //修改realname/headimage/msex字段值
				 login.setN1(nowUser.getStr(GroupMemberModel.HEADIMAGE));
				 login.setN2(nowUser.getStr(GroupMemberModel.REALNAME));
				 login.setN3(nowUser.getStr(GroupMemberModel.SEX));
				 login.setN4(userinfo.getStr(GroupMemberinfoModel.NATIVEPLACE));
				 login.setN5(userinfo.getStr(GroupMemberinfoModel.BIRTHDAY));
				 login.setN6(nowUser.getStr(GroupMemberModel.GROUPIDS));
				 login.setN7(nowUser.getStr(GroupMemberModel.GROUPNAMES));
				 login.setN8(gm!=null?gm.getStr(GroupInfoModel.GNAME):"");
				 login.setN9(gm!=null?gm.getStr(GroupInfoModel.GROUPLOGO):"");
				 login.setN10(gcm!=null?gcm.getStr(GroupContactsModel.CNAMES):"guest");
				 login.setN11(0);
			 }else
			 {
				 type=1;
				 login.setN0(loginName);
				 login.setN1("");
				 login.setN2("游客"+SMSUtils.randomSMSCode(6));
				 login.setN3("男");
				 login.setN4("中国");
				 login.setN5("");
				 login.setN6(nowUser.getStr(GroupMemberModel.GROUPIDS));
				 login.setN7(nowUser.getStr(GroupMemberModel.GROUPNAMES));
				 login.setN8(gm!=null?gm.getStr(GroupInfoModel.GNAME):"");
				 login.setN9(gm!=null?gm.getStr(GroupInfoModel.GROUPLOGO):"");
				 login.setN10("guest");
				 login.setN11(0);
			 }
			 
		}
		
		//保存登陆记录
		new BasicApploginModel().set(BasicApploginModel.ID, RandomUtils.randomCustomUUID())
		.set(BasicApploginModel.VERSION, 0)
		.set(BasicApploginModel.MID, loginName)
		.set(BasicApploginModel.MIDTYPE, type)
		.set(BasicApploginModel.GROUPID, groupid)
		.set(BasicApploginModel.CREATETIME, new Date()).save();
		
		// 删除验证码记录
	//	Db.update(DELSMSAUTHCODESQL,loginName, code);		
		
		response.setInfo(login);
		response.setMessage("登陆成功！");
		renderJson(response);
	}
	
	/**
	 * 登录接口
	 */
	public void login() {
		String loginName = getPara("mid");//手机号码
		String password = getPara("paswd");//密码或者验证码
		String groupid = getPara("groupid");//团体ID
		int type = getParaToInt("type", 0);// 0会员 1游客
		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(loginName, "loginName can not be null")
				.put(password, "password can not be null")
				.put(groupid, "groupid can not be null")
				.put(type, "type can not be null"))) {
			return;
		}
		GroupMemberModel nowUser=null;
		if (type == 0) {
			nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDANDPWDSQL,
					loginName, StringUtils.encodePassword(password, "md5"));
		} else {
			// 检查账户是否已被注册
			if (Db.findFirst(MEMBERMIDSSQL,
					loginName) != null) {
				renderJson(new BaseResponse(Code.ACCOUNT_EXISTS,
						"手机号码已经注册过了！"));
				return;
			}

			// 检查验证码是否有效, 如果业务不需要，则无需保存此段代码
			if (Db.findFirst(SMSAUTHCODESQL,
					loginName, password) == null) {
				renderJson(new BaseResponse(Code.CODE_ERROR, "验证码非法！"));
				return;
			}
			nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,"1000000000");
		}
	
		LoginResponse response = new LoginResponse();
	    LoginResponseEntery login=null;
		if (nowUser == null) {
			response.setCode(Code.FAIL).setMessage(
					"userName or password is error");
			renderJson(response);
			return;
		}
		 GroupMemberinfoModel userinfo=GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,nowUser.getStr(GroupMemberModel.MID));
		 GroupInfoModel gm=GroupInfoModel.dao.findById(groupid);
		 login=new LoginResponseEntery();
		 if(userinfo!=null)
		 {
			 type=0;
			 GroupContactsModel gcm=GroupContactsModel.dao.findFirst(GROUPCONTACTSBYGROUPIDANDMIDSSQL, groupid,loginName);
			 login.setN0(loginName);
//			 login.setN1(userinfo.getStr(GroupMemberinfoModel.HEADIMAGE));
//			 login.setN2(userinfo.getStr(GroupMemberinfoModel.REALNAME));
//			 login.setN3(userinfo.getStr(GroupMemberinfoModel.SEX));
			 //修改realname/headimage/msex字段值
			 login.setN1(nowUser.getStr(GroupMemberModel.HEADIMAGE));
			 login.setN2(nowUser.getStr(GroupMemberModel.REALNAME));
			 login.setN3(nowUser.getStr(GroupMemberModel.SEX));
			 login.setN4(userinfo.getStr(GroupMemberinfoModel.NATIVEPLACE));
			 login.setN5(userinfo.getStr(GroupMemberinfoModel.BIRTHDAY));
			 login.setN6(nowUser.getStr(GroupMemberModel.GROUPIDS));
			 login.setN7(nowUser.getStr(GroupMemberModel.GROUPNAMES));
			 login.setN8(gm.getStr(GroupInfoModel.GNAME));
			 login.setN9(gm.getStr(GroupInfoModel.GROUPLOGO));
			 login.setN10(type==0?gcm.getStr(GroupContactsModel.CNAMES):"guest");
			 login.setN11(0);
			
		 }else
		 {
			 type=1;
			 login.setN0(loginName);
			 login.setN1("");
			 login.setN2("游客"+SMSUtils.randomSMSCode(6));
			 login.setN3("男");
			 login.setN4("中国");
			 login.setN5("");
			 login.setN6(nowUser.getStr(GroupMemberModel.GROUPIDS));
			 login.setN7(nowUser.getStr(GroupMemberModel.GROUPNAMES));
			 login.setN8(gm.getStr(GroupInfoModel.GNAME));
			 login.setN9(gm.getStr(GroupInfoModel.GROUPLOGO));
			 login.setN10("guest");
			 login.setN11(0);
		 }
		 
		//保存登陆记录
		new BasicApploginModel().set(BasicApploginModel.ID, RandomUtils.randomCustomUUID())
		.set(BasicApploginModel.VERSION, 0)
		.set(BasicApploginModel.MID, loginName)
		.set(BasicApploginModel.MIDTYPE, type)
		.set(BasicApploginModel.GROUPID, groupid)
		.set(BasicApploginModel.CREATETIME, new Date()).save();
		response.setInfo(login);
		response.setMessage(userinfo!=null?"login success":"Token is null");
		//response.setToken(TokenManager.getMe().generateToken(nowUser));

		renderJson(response);
	}

	/**
	 * 资料相关的接口
	 */
	public void profile() {
		String method = getRequest().getMethod();
		MemberProfile fb=null;
		if ("get".equalsIgnoreCase(method)) { // 查询资料
			getProfile();
		} else if ("post".equalsIgnoreCase(method)) { // 修改资料
			try {
				fb = getRepuestJosnObject(getRequest().getReader(), MemberProfile.class);// 取出请求接口参数
			} catch (Exception e) {
				e.printStackTrace();
			}
			updateProfile(fb);
		} else {
			render404();
		}
	}

	/**
	 * 查询用户资料
	 */
	private void getProfile() {
		String userId = getPara("mid");
		GroupMemberModel resultUser = null;
		if (StringUtils.isNotEmpty(userId)) {
			resultUser = GroupMemberModel.dao.findById(userId);
		} else {
			resultUser = getUser();
		}

		DatumResponse response = new DatumResponse();

		if (resultUser == null) {
			response.setCode(Code.FAIL).setMessage("user is not found");
		} else {
			GroupMemberinfoModel userinfo = GroupMemberinfoModel.dao.findFirst(
					MEMBERINFOMIDSSQL, resultUser.getStr(GroupMemberModel.MID));
			HashMap<String, Object> map = new HashMap<String, Object>(
					userinfo.getAttrs());
			// map.remove(PASSWORD);
			response.setDatum(map);
		}

		renderJson(response);
	}

	/**
	 * 修改用户资料
	 */
	private void updateProfile(MemberProfile fb) {
		boolean flag = false;
		BaseResponse response = new BaseResponse();
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			renderJson(new BaseResponse(Code.NOT_FOUND));
			return;
		}

		String mid = null,groupid=null,realName=null,email=null,sex=null,mnativeplace=null,mbirthday=null;
		
		if (fb != null) {
			mid = fb.getTel();
			groupid= fb.getGroupid();
			realName=fb.getMname();
			email=fb.getEmail();
			sex=fb.getMsex();
			mnativeplace=fb.getMnativeplace();
			mbirthday=fb.getMbirthday();
		}else
			return;
		// 校验参数, 确保不能为空
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				)) {
			return;
		}
		GroupMemberinfoModel userinfo = GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL,mid);
		//通过mid查询会员基础数据
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,mid);
	
		if (StringUtils.isNotEmpty(realName)&&realName!=null) {
//			userinfo.set(GroupMemberinfoModel.REALNAME, realName);
			nowUser.set(GroupMemberModel.REALNAME, realName);
			flag = true;
		}

		if (StringUtils.isNotEmpty(email)&&email!=null) {
			userinfo.set(GroupMemberinfoModel.EMAIL, email);
			flag = true;
		}
	
		if (StringUtils.isNotEmpty(sex)&&sex!=null) {
//			userinfo.set(GroupMemberinfoModel.SEX, sex);
			nowUser.set(GroupMemberModel.SEX, sex);
			flag = true;
		}

		if (StringUtils.isNotEmpty(mnativeplace)&&mnativeplace!=null) {
			userinfo.set(GroupMemberinfoModel.NATIVEPLACE, mnativeplace);
			flag = true;
		}

		if (StringUtils.isNotEmpty(mbirthday)&&mbirthday!=null) {
			userinfo.set(GroupMemberinfoModel.BIRTHDAY, mbirthday);
			flag = true;
		}

		if (flag) {
			boolean update = userinfo.update();
			boolean updateuser = nowUser.update();//修改用户基础资料
			renderJson(response.setCode(update ? Code.SUCCESS : Code.FAIL)
					.setMessage(update ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
		} else {
			renderArgumentError("must set profile");
		}
	}

	/**
	 * 修改密码
	 */
	public void password() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 根据用户id，查出这个用户的密码，再跟传递的旧密码对比，一样就更新，否则提示旧密码错误
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		
		String code = getPara("code");
		String newPwd = getPara("newpwd");
		if (!notNull(Require.me().put(code, "code can not be null")
				.put(newPwd, "newPwd can not be null")
				.put(groupid, "groupid can not be null")
				.put(mid, "mid can not be null")
				.put(did, "did can not be null"))) {
			return;
		}
		// 检查验证码是否有效
		//if (Db.findFirst(SMSAUTHCODESQL,mid, code) == null) {
			//renderJson(new BaseResponse(Code.CODE_ERROR, "code is invalid"));
			///return;
		//}
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL, mid);
		boolean flag = nowUser.set(GroupMemberModel.PWD,StringUtils.encodePassword(newPwd, "md5")).update();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
		
	}

	/**
	 * 修改头像接口 /api/account/avatar
	 */
	public void avatar() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			renderJson(new BaseResponse(Code.NOT_FOUND));
			return;
		}
		// 根据用户id，查出这个用户的密码，再跟传递的旧密码对比，一样就更新，否则提示旧密码错误
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		String avatar = getPara("mheadimage");
		if (!notNull(Require.me().put(avatar, "avatar url can not be null").put(groupid, "groupid can not be null")
				.put(mid, "mid can not be null")
				.put(did, "did can not be null"))) {
			return;
		}
	//	GroupMemberinfoModel gminfo=GroupMemberinfoModel.dao.findFirst(MEMBERINFOMIDSSQL, mid);
		//通过mid查询会员基础数据
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,mid);
//		boolean flag = gminfo.set(GroupMemberinfoModel.HEADIMAGE, avatar).update();
		boolean flag = nowUser.set(GroupMemberModel.HEADIMAGE, avatar).update();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,flag ? Code.FINAL_STRING_SUCCESS:"null"));
	}
	public void getuserheadimage()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			renderJson(new BaseResponse(Code.NOT_FOUND));
			return;
		}
		// 根据用户id，查出这个用户的密码，再跟传递的旧密码对比，一样就更新，否则提示旧密码错误
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		if (!notNull(Require.me().put(groupid, "groupid can not be null")
				.put(mid, "mid can not be null")
				.put(did, "did can not be null"))) {
			return;
		}
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,mid);

		renderJson(new BaseResponse(nowUser!=null ? Code.SUCCESS : Code.FAIL,nowUser!=null ?nowUser.getStr(GroupMemberModel.HEADIMAGE) : Code.FINAL_STRING_ERROR));
	}
	/**
	 * 找回密码接口 /api/account/findpaswdbymidandcode
	 */
	public void findpaswdbymidandcode() {
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String groupid = getPara("groupid");// 团体ID
		String loginName = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		String newpwd = getPara("newpaswd");//新密码
		int code = getParaToInt("code", 0);// 手机验证码
		
		// 校验必填项参数
		if (!notNull(Require.me().put(loginName, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(newpwd, "newpwd can not be null")
				.put(code, "code can not be null")
		)) {
			return;
		}
		// 检查验证码是否有效, 如果业务不需要，则无需保存此段代码
		if (Db.findFirst(
				SMSAUTHCODESQL,
				loginName, code) == null) {
			renderJson(new BaseResponse(Code.CODE_ERROR, "验证码非法！"));
			return;
		}
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,loginName);
		boolean flag =nowUser.set(GroupMemberModel.PWD, StringUtils.encodePassword(newpwd, "md5")).update();
		renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL,flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	}
	/**
	 * 查询会员详细信息接口 /api/account/getmemberinfobymid
	 */
	public void getmemberinfobymid()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		String tomid = getPara("tomid");// 查询会员手机号码
		String togroupid = getPara("togroupid");// 查询会员手机号码
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
		)) {
			return;
		}
		GroupMemberInfoEntey gmi=null;
		if(tomid!=null&&togroupid!=null)
		{
			gmi=getMemberInfoEntey(mid,togroupid,0,tomid);
		}else
		{
			gmi=getMemberInfoEntey(mid,groupid,0,null);
		}
		DatumResponse response = new DatumResponse();
		if (gmi == null) {
			response.setCode(Code.FAIL).setMessage("此用户无详细信息！请联系秘书处！");
		} else {
			response.setDatum(gmi);
		}

		renderJson(response);
	}
	/**
	 * 查询会员消息列表接口 /api/account/getmembermessagelist
	 */
	public void getmembermessagelist()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		
		int size = getParaToInt("pagesize", 0);// 分页大小
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
				.put(size, "pagesize can not be null")
		)) {
			return;
		}
		List<GroupMemberSysSmsListEntey> listgmsms=null;
		List<SystemReviewModel> reviewlist= SystemReviewModel.dao.find(GROUPMEMBERREVIEW_LIST_SQL,groupid, mid,size);
		boolean flag=false;
		if(reviewlist!=null)
		{
			
		}
		
		DataResponse response= new DataResponse();	
		response.setCode(flag?Code.SUCCESS:Code.ERROR);
		response.setMessage(flag?"SUCCESS":"没有消息列表");
		response.setData(listgmsms);
		renderJson(response);
		
	}
	/**
	 * 会员新增公司信息接口 /api/account/addmycompanyinfo
	 */
	public void addmycompanyinfo()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		MemberCompanyinfoEntry mc=null;
		String groupid=null,mid=null,did=null,togroupid=null,togroupname=null,comname=null,comlogo=null,comduty=null,comproperty=null,comscale=null,comindustry=null,comtellphone=null,comemail=null,comaddress=null,comurl=null,comecurl=null, comment=null;
		try {
			mc = getRepuestJosnObject(getRequest().getReader(), MemberCompanyinfoEntry.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(mc!=null)
		{
			groupid=mc.getGroupid();
			mid=mc.getMid();
			did=mc.getDid();
			togroupid=mc.getTogroupid();
			togroupname=mc.getTogroupname();
			comname=mc.getComname();
			comlogo=mc.getComlogo();
			comduty=mc.getComduty();
			comproperty=mc.getComproperty();
			comscale=mc.getComscale();
			comindustry=mc.getComindustry();
			comtellphone=mc.getComtellphone();
			comemail=mc.getComemail();
			comaddress=mc.getComaddress();
			comurl=mc.getComurl();
			comecurl=mc.getComecurl();
			comment=mc.getComment();
		}else
			return;
		
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
				.put(togroupid, "togroupid can not be null")
				.put(togroupname, "togroupname can not be null")
				.put(comname, "comname can not be null")
		)) {
			return;
		}
		BaseResponse response = new BaseResponse();
		GroupMemberModel nowUser = GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,mid);
		boolean flag=new GroupMemberCompanyModel().set(GroupMemberCompanyModel.ID, getCreateUUId())
				.set(GroupMemberCompanyModel.VERSION, 0)
				.set(GroupMemberCompanyModel.ORDERIDS, 0)
				.set(GroupMemberCompanyModel.GROUPIDS, togroupid)
				.set(GroupMemberCompanyModel.GROUPNAMES, togroupname)
				.set(GroupMemberCompanyModel.MID, mid)
				.set(GroupMemberCompanyModel.COMNAME, comname)
				.set(GroupMemberCompanyModel.COMLOGO, comlogo)
				.set(GroupMemberCompanyModel.COMDUTY, comduty)
				.set(GroupMemberCompanyModel.COMPROPERTY, comproperty)
				.set(GroupMemberCompanyModel.COMSCALE, comscale)
				.set(GroupMemberCompanyModel.COMINDUSTRY, comindustry)
				.set(GroupMemberCompanyModel.COMTEL, comtellphone)
				.set(GroupMemberCompanyModel.COMEMAIL, comemail)
				.set(GroupMemberCompanyModel.CADDRESS, comaddress)
				.set(GroupMemberCompanyModel.COMURL, comurl)
				.set(GroupMemberCompanyModel.COMECURL,comecurl)
				.set(GroupMemberCompanyModel.COMMENTS, comment)
				.set(GroupMemberCompanyModel.CREATETIME, DateUtils.getDate())
				.set(GroupMemberCompanyModel.MEMBERID, nowUser!=null?nowUser.get(GroupMemberModel.ID):"")
				.save();

		renderJson(response.setCode(flag ? Code.SUCCESS : Code.FAIL)
				.setMessage(flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	}
	/**
	 * 会员编辑公司信息接口 /api/account/editmycompanyinfo
	 */
	public void editmycompanyinfo()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		MemberCompanyinfoEntry mc=null;
		String groupid=null,mid=null,did=null,comid=null,togroupid=null,togroupname=null,comname=null,comlogo=null,comduty=null,comproperty=null,comscale=null,comindustry=null,comtellphone=null,comemail=null,comaddress=null,comurl=null,comecurl=null, comment=null;
		try {
			mc = getRepuestJosnObject(getRequest().getReader(), MemberCompanyinfoEntry.class);// 取出请求接口参数
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(mc!=null)
		{
			groupid=mc.getGroupid();
			mid=mc.getMid();
			did=mc.getDid();
			comid=mc.getComid();
			togroupid=mc.getTogroupid();
			togroupname=mc.getTogroupname();
			comname=mc.getComname();
			comlogo=mc.getComlogo();
			comduty=mc.getComduty();
			comproperty=mc.getComproperty();
			comscale=mc.getComscale();
			comindustry=mc.getComindustry();
			comtellphone=mc.getComtellphone();
			comemail=mc.getComemail();
			comaddress=mc.getComaddress();
			comurl=mc.getComurl();
			comecurl=mc.getComecurl();
			comment=mc.getComment();
		}else
			return;
		
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
				.put(comid, "comid can not be null")
		)) {
			return;
		}
		BaseResponse response = new BaseResponse();
		boolean flag=false;
		GroupMemberCompanyModel gmcm= GroupMemberCompanyModel.dao.findById(comid);
		if (StringUtils.isNotEmpty(togroupid)) {
			gmcm.set(GroupMemberCompanyModel.GROUPIDS, togroupid);
			flag = true;
		}
		
		if (StringUtils.isNotEmpty(togroupname)) {
			gmcm.set(GroupMemberCompanyModel.GROUPNAMES, togroupname);
			flag = true;
		}
		
		if (StringUtils.isNotEmpty(comname)) {
			gmcm.set(GroupMemberCompanyModel.COMNAME, comname);
			flag = true;
		}
		if (StringUtils.isNotEmpty(comduty)) {
			gmcm.set(GroupMemberCompanyModel.COMDUTY, comduty);
			flag = true;
		}
		if (StringUtils.isNotEmpty(comlogo)) {
			gmcm.set(GroupMemberCompanyModel.COMLOGO, comlogo);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comproperty)) {
			gmcm.set(GroupMemberCompanyModel.COMPROPERTY, comproperty);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comscale)) {
			gmcm.set(GroupMemberCompanyModel.COMSCALE, comscale);
			flag = true;
		}
	
		if (StringUtils.isNotEmpty(comindustry)) {
			gmcm.set(GroupMemberCompanyModel.COMINDUSTRY, comindustry);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comtellphone)) {
			gmcm.set(GroupMemberCompanyModel.COMTEL, comtellphone);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comemail)) {
			gmcm.set(GroupMemberCompanyModel.COMEMAIL, comemail);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comproperty)) {
			gmcm.set(GroupMemberCompanyModel.CADDRESS, comaddress);
			flag = true;
		}

		if (StringUtils.isNotEmpty(comscale)) {
			gmcm.set(GroupMemberCompanyModel.COMURL, comurl);
			flag = true;
		}
		if (StringUtils.isNotEmpty(comscale)) {
			gmcm.set(GroupMemberCompanyModel.COMECURL, comecurl);
			flag = true;
		}
		if (StringUtils.isNotEmpty(comindustry)) {
			gmcm.set(GroupMemberCompanyModel.COMMENTS, comment);
			flag = true;
		}
		
		if (flag) {
			boolean update = gmcm.update();
			renderJson(response.setCode(update ? Code.SUCCESS : Code.FAIL)
					.setMessage(update ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
		} else {
			renderArgumentError("must set profile");
		}
	}
	/**
	 * 会员删除公司信息接口 /api/account/delmycompanyinfo
	 */
	public void delmycompanyinfo()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		
		String comid = getPara("comid");//公司信息ID
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
				.put(comid, "comid can not be null")
		)) {
			return;
		}
		boolean flag = GroupMemberCompanyModel.dao.deleteById(comid);
		// 返回数据
        renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL, flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	}
	/**
	 * 获取会员的所加入的团体列表接口 /api/account/getmygroupinfolist
	 */
	public void getmygroupinfolist()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String groupid = getPara("groupid");// 团体ID
		String mid = getPara("mid");// 会员手机号码
		String did = getPara("did");//类型ID
		
		int size = getParaToInt("pagesize", 0);// 分页大小
		// 校验必填项参数
		if (!notNull(Require.me().put(mid, "mid can not be null")
				.put(groupid, "groupid can not be null")
				.put(did, "did can not be null")
				.put(size, "pagesize can not be null")
		)) {
			return;
		}
		GroupMemberModel  user= GroupMemberModel.dao.findFirst(MEMBERMIDSSQL, mid);
		List<MyGroupInfoEntey> mylist=null;
		boolean flag=false;
		if(StringUtils.isNotEmpty(user.getStr(GroupMemberModel.GROUPIDS)))
		{
			flag=true;
			mylist=new ArrayList<MyGroupInfoEntey>();
			GroupInfoModel ginfo=null;
			MyGroupInfoEntey gie=null;
			if(user.getStr(GroupMemberModel.GROUPIDS).indexOf(",") != -1)
			{
				String[] temp=user.getStr(GroupMemberModel.GROUPIDS).split(",");
				for(int i=0;i<temp.length;i++)
				{
					ginfo=GroupInfoModel.dao.findById(temp[i]);
					gie=new MyGroupInfoEntey();
					gie.setN0(temp[i]);
					gie.setN1(ginfo.getStr(GroupInfoModel.GROUPLOGO));
					gie.setN2(ginfo.getStr(GroupInfoModel.GNAME));
					gie.setN3(ginfo.getNumber(GroupInfoModel.ORDERIDS).intValue());
					
					mylist.add(gie);
				}
				
			}else
			{
				ginfo=GroupInfoModel.dao.findById(user.getStr(GroupMemberModel.GROUPIDS));
				gie=new MyGroupInfoEntey();
				gie.setN0(user.getStr(GroupMemberModel.GROUPIDS));
				gie.setN1(ginfo.getStr(GroupInfoModel.GROUPLOGO));
				gie.setN2(ginfo.getStr(GroupInfoModel.GNAME));
				gie.setN3(ginfo.getNumber(GroupInfoModel.ORDERIDS).intValue());
				
				mylist.add(gie);
			}
		}
		
		DataResponse response= new DataResponse();	
		response.setCode(flag?Code.SUCCESS:Code.ERROR);
		response.setMessage(flag?"SUCCESS":"没加入团体信息列表");
		response.setData(mylist);
		renderJson(response);
	}
	/**
	 * get orders
	 */
	public void getmyorderlist()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 会员手机号码
		String GROUPID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型
		int SIZE = getParaToInt("pagesize", 0);// 分页大小
		int TYPE = getParaToInt("type", 0);// type
		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(DID, "did can not be null")
				.put(GROUPID, "groupid can not be null")
				.put(TYPE, "groupid can not be null")
				.put(SIZE, "pagesize can not be null"))) {
			return;
		}
		List<GroupEcprodOrderModel> geodmOrderModels=GroupEcprodOrderModel.dao.find(GROUPMEMBER_ORDER_LIST_SQL, GROUPID,MID,TYPE,SIZE);
		List<GroupEcOrder> geolist=null;
		GroupEcOrder geo=null;
		GroupEcprodinfoModel gepinfo=null;
		if(geodmOrderModels!=null)
		{
			geolist=new ArrayList<GroupEcOrder>();
			for(GroupEcprodOrderModel gem:geodmOrderModels)
			{
				geo=new GroupEcOrder();
				geo.setN0(gem.getStr(GroupEcprodOrderModel.ID));
				gepinfo=GroupEcprodinfoModel.dao.findById(gem.getStr(GroupEcprodOrderModel.ECPRODID));
				geo.setN1(gepinfo!=null?gepinfo.getStr(GroupEcprodinfoModel.ECPRODHOMEIMAGE):"null");
				geo.setN2(gepinfo!=null?gepinfo.getStr(GroupEcprodinfoModel.ECPRODNAME):"");
				geo.setN3(gem.getDouble(GroupEcprodOrderModel.OSUMPRICE));
				geo.setN4(gem.getNumber(GroupEcprodOrderModel.OSUMNUM).intValue());
				geo.setN5(gem.getStr(GroupEcprodOrderModel.MCAID));
				geo.setN6(gem.getStr(GroupEcprodOrderModel.OMESSAGE));
				
				geolist.add(geo);
			}
		}
		
		DataResponse response= new DataResponse();	
		response.setCode(geodmOrderModels!=null?Code.SUCCESS:Code.ERROR);
		response.setMessage(geodmOrderModels!=null?"SUCCESS":"没订单信息列表");
		response.setData(geolist);
		renderJson(response);
	}
	/**
	 * del my order by ids
	 */
	public void delmyorderinfobyid()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 会员手机号码
		String GROUPID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型
		String OIDS= getPara("oids");// IDS
		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(DID, "did can not be null")
				.put(GROUPID, "groupid can not be null")
				.put(OIDS, "OIDS can not be null"))) {
			return;
		}
		boolean flag = GroupEcprodOrderModel.dao.deleteById(OIDS);
		// 返回数据
        renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL, flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	}
	/**
	 * 获取会员在团体的职位
	 */
	public void getmemberofgroupjopbymidandgroupid()
	{
		if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
			render404();
			return;
		}
		// 必填信息
		String MID = getPara("mid");// 会员手机号码
		String GROUPID = getPara("groupid");// 团体ID
		String DID = getPara("did");// 类型

		// 校验必填项参数
		if (!notNull(Require.me().put(MID, "mid can not be null")
				.put(DID, "did can not be null")
				.put(GROUPID, "groupid can not be null"))) {
			return;
		}
		 GroupContactsModel gcm=GroupContactsModel.dao.findFirst(GROUPCONTACTSBYGROUPIDANDMIDSSQL, GROUPID,MID);
		 
		// 返回数据
        renderJson(new BaseResponse(gcm!=null ? Code.SUCCESS : Code.FAIL, gcm!=null ? gcm.getStr(GroupContactsModel.CNAMES): "游客"));
	}
	/**
	 * 发送请求增加好友的消息
	 */
	public void addmemberfriendssms()
	{
		 if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			// 必填信息
			String MID = getPara("mid");// 会员手机号码
			String GROUPID = getPara("groupid");// 团体ID
			String DID = getPara("did");// 类型
			String TOMID= getPara("tomid");// 增加好友手机号码
			String tempids=null;
			// 校验必填项参数
			if (!notNull(Require.me().put(MID, "mid can not be null")
					.put(DID, "did can not be null")
					.put(GROUPID, "groupid can not be null")
					.put(TOMID, "tomid can not be null"))) {
				return;
			}
			Boolean flag=new GroupMemberModelFriendsSms()
			.set(GroupMemberModelFriendsSms.ID, getCreateUUId())
			.set(GroupMemberModelFriendsSms.FROMMIDS, MID)
			.set(GroupMemberModelFriendsSms.TOMIDS, TOMID)
			.set(GroupMemberModelFriendsSms.STATUS, 0)
			.set(GroupMemberModelFriendsSms.SMSFLAG, 0)
			.set(GroupMemberModelFriendsSms.CREATETIME, DateUtils.getDate()).save();
			
			if(flag)
			{
				tempids=getCreateUUId();
				//保存推送信息访问地址
				new SystemJpushUrlInfoModel().set(SystemJpushUrlInfoModel.ID, tempids)
				.set(SystemJpushUrlInfoModel.TYPE, 3)
				.set(SystemJpushUrlInfoModel.WEBURL, "/cocapi/app/html/first_frame/addmycontacts_list.html")
				.set(SystemJpushUrlInfoModel.REQPARAM, "")
				.set(SystemJpushUrlInfoModel.CREATETIME,DateUtils.getDate()).save();

				PushUtils.busSendPush(GROUPID,tempids,"您有一条新增好友信息，请注意查收！",TOMID);
			}
			// 返回数据
	        renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL, flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	}
	/**
	 * 获取会员好友信息列表
	 * */
	 public void getmemberfriendsinfolist()
	 {
		 if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			// 必填信息
			String MID = getPara("mid");// 会员手机号码
			String GROUPID = getPara("groupid");// 团体ID
			String DID = getPara("did");// 类型
			int SIZE = getParaToInt("pagesize", 0);// 分页大小
			int CURCOUNT = getParaToInt("curCount", 0);// 当前显示数量
			// 校验必填项参数
			if (!notNull(Require.me().put(MID, "mid can not be null")
					.put(DID, "did can not be null")
					.put(GROUPID, "groupid can not be null")
					.put(CURCOUNT, "curCount can not be null")
					.put(SIZE, "pagesize can not be null"))) {
				return;
			}
			GroupMemberModelFriends gmmf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_SQL,GROUPID,MID);
			List<MemberFriendsEntey> listmfe=new ArrayList<MemberFriendsEntey>();
			MemberFriendsEntey mfe=null;
			String[] tempmids=null;
			GroupMemberModel gmm=null;
			if(gmmf!=null)
			{
				if(gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS).indexOf(",")!=-1)
				{
					tempmids=gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS).split(",");
					for(String tel:tempmids)
					{
						gmm=GroupMemberModel.dao.findFirst(GROUPMEMBER_FRIENDS_INFO_SQL,tel);
						if(gmm!=null)
						{
							mfe=new MemberFriendsEntey();
							mfe.setN0(gmm.getStr(GroupMemberModel.ID));
							mfe.setN1(gmm.getStr(GroupMemberModel.MID));
							mfe.setN2(gmm.getStr(GroupMemberModel.REALNAME));
							mfe.setN3(gmm.getStr(GroupMemberModel.HEADIMAGE));
							mfe.setN4(gmm.getStr(GroupMemberModel.COMNAME)!=null?gmm.getStr(GroupMemberModel.COMNAME):"");
							listmfe.add(mfe);
						}
					}
				}else if(gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS).length()==11)
				{
					gmm=GroupMemberModel.dao.findFirst(GROUPMEMBER_FRIENDS_INFO_SQL,gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS));
					if(gmm!=null)
					{
						mfe=new MemberFriendsEntey();
						mfe.setN0(gmm.getStr(GroupMemberModel.ID));
						mfe.setN1(gmm.getStr(GroupMemberModel.MID));
						mfe.setN2(gmm.getStr(GroupMemberModel.REALNAME));
						mfe.setN3(gmm.getStr(GroupMemberModel.HEADIMAGE));
						mfe.setN4(gmm.getStr(GroupMemberModel.COMNAME)!=null?gmm.getStr(GroupMemberModel.COMNAME):"");
						listmfe.add(mfe);
					}
				}else
				{
					listmfe=null;
				}
			}
			DataResponse response= new DataResponse();	
			response.setCode(listmfe!=null?Code.SUCCESS:Code.ERROR);
			response.setMessage(listmfe!=null?"SUCCESS":"没好友信息");
			response.setData(listmfe);
			renderJson(response);
	 }
	 /**
	  * 通过好友名字来查询好友信息
	  */
	 public void queryMemberFriendsinfoBylavalue()
	 {
			if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			SeachValue fb = null;
			String groupid=null,mid=null,did=null,likevalue=null;// 搜索关键字
			int size=0,cur=0;
			
			try {
				fb = getRepuestJosnObject(getRequest().getReader(), SeachValue.class);// 取出请求接口参数
			} catch (Exception e) {
				e.printStackTrace();
			}
			if (fb != null) {
				groupid=fb.getGroupid();
				mid=fb.getMid();
				did=fb.getDid();
				size=fb.getPagesize();
				cur=fb.getCurcount();
				likevalue = fb.getLikevalue();
			}else
			{
				renderArgumentError("likevalue can not be null");
				return;
			}
			if (StringUtils.isEmpty(likevalue)) {
				renderArgumentError("likevalue can not be null");
				return;
			}
			// 校验参数, 确保不能为空
			if (!notNull(Require.me().put(did, "did can not be null")
					.put(mid, "mid can not be null")
					.put(groupid, "groupid can not be null")
					.put(likevalue, "likevalue can not be null")
					.put(size, "pagesize can not be null"))) {
				return;
			}
			
			List<MemberFriendsEntey> listmfe=new ArrayList<MemberFriendsEntey>();
			MemberFriendsEntey mfe=null;
			GroupMemberModelFriends gmmf=null;
			String temp=mid;
			List<GroupMemberModel> listgmm=GroupMemberModel.dao.find("SELECT yg.ids,yg.mids,yg.realname,yg.headimage,ygmc.comname as comname from y_group_member yg LEFT JOIN y_group_member_company ygmc ON yg.mids=ygmc.mids where 1=1 and yg.realname LIKE '%"+likevalue+"%'");
			if(listgmm!=null)
			{
				for(GroupMemberModel gmm:listgmm)
				{
					gmmf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_ASSAME_SQL,groupid,mid,gmm.getStr(GroupMemberModel.MID));
					if(gmmf!=null)
					{
						if(!gmm.getStr(GroupMemberModel.MID).equals(temp))
						{
							temp=gmm.getStr(GroupMemberModel.MID);
							mfe=new MemberFriendsEntey();
							mfe.setN0(gmm.getStr(GroupMemberModel.ID));
							mfe.setN1(gmm.getStr(GroupMemberModel.MID));
							mfe.setN2(gmm.getStr(GroupMemberModel.REALNAME));
							mfe.setN3(gmm.getStr(GroupMemberModel.HEADIMAGE));
							mfe.setN4(gmm.getStr(GroupMemberModel.COMNAME)!=null?gmm.getStr(GroupMemberModel.COMNAME):"");
							listmfe.add(mfe);
						}
					}
						
				}
			}
			DataResponse response= new DataResponse();	
			response.setCode(listmfe!=null?Code.SUCCESS:Code.ERROR);
			response.setMessage(listmfe!=null?"SUCCESS":"没好友信息");
			response.setData(listmfe);
			renderJson(response);
	 }
	 
	 /**
	  * 通过好友手机号码查询是否已经增加
	  */
	 public void queryMemberFriendsinfoBymids()
	 {
		 if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			// 必填信息
			String MID = getPara("mid");// 会员手机号码
			String GROUPID = getPara("groupid");// 团体ID
			String DID = getPara("did");// 类型
			String TOMID= getPara("tomid");// 增加好友手机号码
			// 校验必填项参数
			if (!notNull(Require.me().put(MID, "mid can not be null")
					.put(DID, "did can not be null")
					.put(GROUPID, "groupid can not be null")
					.put(TOMID, "tomid can not be null"))) {
				return;
			}
			GroupMemberModelFriends gmmf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_ASSAME_SQL,GROUPID,MID,TOMID);
			// 返回数据
	        renderJson(new BaseResponse(gmmf==null ? Code.SUCCESS : Code.FAIL, gmmf==null ? "可增加好友": "已经增加为好友"));
			
	 }
	/**
	 * 获取我的新增好友消息列表
	 * */
	 public void getmembernewaddfriendsmessagelist()
	 {
		 if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			// 必填信息
			String MID = getPara("mid");// 会员手机号码
			String GROUPID = getPara("groupid");// 团体ID
			String DID = getPara("did");// 类型
			int SIZE = getParaToInt("pagesize", 0);// 分页大小
			int CURCOUNT = getParaToInt("curCount", 0);// 当前显示数量
			// 校验必填项参数
			if (!notNull(Require.me().put(MID, "mid can not be null")
					.put(DID, "did can not be null")
					.put(GROUPID, "groupid can not be null")
					.put(CURCOUNT, "curCount can not be null")
					.put(SIZE, "pagesize can not be null"))) {
				return;
			}
			List<GroupMemberModelFriendsSms> listgmmfs=GroupMemberModelFriendsSms.dao.find(GROUPMEMBER_FRIENDS_SMSLIST_SQL, MID,0,CURCOUNT,SIZE+CURCOUNT);
			List<MemberFriendsAddsmsEntey> listmfae=new ArrayList<MemberFriendsAddsmsEntey>();
			MemberFriendsAddsmsEntey mfae=null;
			GroupMemberModel gmm=null;
			if(listgmmfs!=null)
			{
				if(listgmmfs.size()>0)
				{

					for(GroupMemberModelFriendsSms gmmfs:listgmmfs)
					{
						mfae=new MemberFriendsAddsmsEntey();
						mfae.setN0(gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS));
						gmm=GroupMemberModel.dao.findFirst(MEMBERMIDSSQL,gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS));
						mfae.setN1(gmm.getStr(GroupMemberModel.REALNAME));
						mfae.setN2(gmm.getStr(GroupMemberModel.HEADIMAGE));
						mfae.setN3(gmm.getStr(GroupMemberModel.GROUPNAMES));
						mfae.setN4(gmmfs.getStr(GroupMemberModelFriendsSms.ID));
						listmfae.add(mfae);
					}
				}
			}
			List<SystemJpushUrlInfoModel> listsjuim=SystemJpushUrlInfoModel.dao.find("select * from y_system_jpush_urlinfo where 1=1 and  groupid=? and type=?", GROUPID,3);
			if(listsjuim!=null)
			{
				for(SystemJpushUrlInfoModel sjum:listsjuim)
				{
					new SystemJpushUserReadInfoModel().set(SystemJpushUserReadInfoModel.ID, getCreateUUId())
					.set(SystemJpushUserReadInfoModel.MIDS, MID)
					.set(SystemJpushUserReadInfoModel.PUSHIDS, sjum.getStr(SystemJpushUrlInfoModel.ID)).set(SystemJpushUserReadInfoModel.CREATETIME, DateUtils.getDate()).save();
				}
			}
			DataResponse response= new DataResponse();	
			response.setCode(listmfae!=null?Code.SUCCESS:Code.ERROR);
			response.setMessage(listmfae!=null?"SUCCESS":"没新增好友信息");
			response.setData(listmfae);
			renderJson(response);
	 }
	 
	 /**
	 * 修改请求好友信息状态
	* */
	 public void updatemembernewaddfriendsmessagestate()
	 {
		 if (!"post".equalsIgnoreCase(getRequest().getMethod())) {
				render404();
				return;
			}
			// 必填信息
			String MID = getPara("mid");// 会员手机号码
			String GROUPID = getPara("groupid");// 团体ID
			String DID = getPara("did");// 类型
			String SMSIDS = getPara("sid");// 消息IDS
			int TYPE = getParaToInt("type", 0);// 0通过1不通过
			
			// 校验必填项参数
			if (!notNull(Require.me().put(MID, "mid can not be null")
					.put(DID, "did can not be null")
					.put(GROUPID, "groupid can not be null")
					.put(TYPE, "type can not be null")
					.put(SMSIDS, "sid can not be null"))) {
				return;
			}
			GroupMemberModelFriendsSms gmmfs=GroupMemberModelFriendsSms.dao.findById(SMSIDS);
			GroupMemberModelFriends gf=null;
			Boolean flag=gmmfs.set(GroupMemberModelFriendsSms.SMSFLAG, 1).set(GroupMemberModelFriendsSms.STATUS, TYPE).set(GroupMemberModelFriendsSms.UPDATETIME, DateUtils.getDate()).update();
			if(TYPE==0)
			{
				GroupMemberModelFriends gmmf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_BYMIDS,GROUPID,MID);
				GroupMemberModelFriends gmmfform=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_BYMIDS,GROUPID,gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS));
				if(gmmf!=null)
				{
					if(gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS)!=null)
					{
						gf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_ASSAME_SQL,GROUPID,MID,gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS));
						if(gf==null)
							flag=gmmf.set(GroupMemberModelFriends.FRIENDSMIDS,gmmf.getStr(GroupMemberModelFriends.FRIENDSMIDS)+","+gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS)).update();
					}else
						flag=gmmf.set(GroupMemberModelFriends.FRIENDSMIDS, gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS)).update();
					
				}else{
					flag=new GroupMemberModelFriends().set(GroupMemberModelFriends.ID,getCreateUUId())
					.set(GroupMemberModelFriends.GROUPIDS, GROUPID)
					.set(GroupMemberModelFriends.MID, MID)
					.set(GroupMemberModelFriends.FRIENDSMIDS, gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS)).save();
					
				}
				if(gmmfform!=null)
				{
					if(gmmfform.getStr(GroupMemberModelFriends.FRIENDSMIDS)!=null)
					{
						gf=GroupMemberModelFriends.dao.findFirst(GROUPMEMBER_FRIENDS_ASSAME_SQL,GROUPID,gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS),MID);
						if(gf==null)
							flag=gmmfform.set(GroupMemberModelFriends.FRIENDSMIDS,gmmfform.getStr(GroupMemberModelFriends.FRIENDSMIDS)+","+MID).update();
					}else
						flag=gmmfform.set(GroupMemberModelFriends.FRIENDSMIDS, MID).update();
					
				}else{
					flag=new GroupMemberModelFriends().set(GroupMemberModelFriends.ID,getCreateUUId())
					.set(GroupMemberModelFriends.GROUPIDS, GROUPID)
					.set(GroupMemberModelFriends.MID, gmmfs.getStr(GroupMemberModelFriendsSms.FROMMIDS))
					.set(GroupMemberModelFriends.FRIENDSMIDS,MID).save();
					
				}
			}
			// 返回数据
	        renderJson(new BaseResponse(flag ? Code.SUCCESS : Code.FAIL, flag ? Code.FINAL_STRING_SUCCESS: Code.FINAL_STRING_ERROR));
	 }
	 
}

